<?php
/**
 * @package model.dao
 * @author Mufeed
 * @copyright Copyright &copy; 2011, synergyquad
 * @since 1.0 - Oct-18-2012
 * @version 1.1
 */

require_once 'vo/UserVO.php';
require_once 'vo/AdminVO.php';
require_once 'vo/ForumVO.php';
require_once 'vo/CompanyVO.php';
require_once 'vo/AttachmentVO.php';
require_once 'vo/NotificationVO.php';


class ModelDAOImpl
{
	
	/**
	 * @access public
	 * @staticvar Instance of MemberDAOImpl
	 */
	public static $_instance	= null;
	
	
	/**
	 * <p>Get an instance of MakeDAOImpl</p>
	 * 
	 * @access public
	 * @return MakeDAOImpl Instance of MakeDAOImpl
	 */
	public static function getInstance() 
	{
		if(self::$_instance == null) 
		{
			self::$_instance = new ModelDAOImpl();
		}
		
		return self::$_instance;
	}
        
        public function getModelByMake($make_id)
        {
            $arrayCollection = null;
            
            $query = "SELECT model_id, model_name FROM vehicle_model WHERE make_id=".$make_id;
            $result = mysql_query($query) or die($query);
            $resultArray = array();
            while($row =  mysql_fetch_object($result))
            {
                array_push($resultArray, $row);
            }
            $arrayCollection = new ArrayCollection();
            $arrayCollection->models = $resultArray;
            return $arrayCollection;
        }
	
	public function getAllModels($start, $count, $searchKey) 
	{
		$arrayCollection = null;
		$where = '';
		$arrayCollection = null;
		$query_data = "SELECT vehicle_model.*,vehicle_make.make,vehicle_make.make_id  FROM vehicle_model LEFT JOIN vehicle_make ON vehicle_model.make_id=vehicle_make.make_id  WHERE (vehicle_model.deleted=0 AND vehicle_model.active=1) ";
		if($searchKey)
			$where= "AND (vehicle_model.model_name LIKE '%".$searchKey."%' OR vehicle_model.updated_by LIKE '%".$searchKey."%' OR vehicle_model.created_by LIKE '%".$searchKey."%' OR vehicle_make.make LIKE '%".$searchKey."%')";
		$query_data.=$where."LIMIT ".$start.",".$count;

		$result_data = mysql_query($query_data) or die($query_data);
		
		$query_count = "SELECT COUNT(*) as count FROM vehicle_model LEFT JOIN vehicle_make ON vehicle_model.make_id=vehicle_make.make_id WHERE (vehicle_model.deleted=0 AND vehicle_model.active=1) ".$where;
		$result_count = mysql_fetch_array(mysql_query($query_count));
		
		$resultArray = array();

		while ($row =  mysql_fetch_object($result_data))
        {
		
			array_push($resultArray, $row);

		}
		
		$arrayCollection = new ArrayCollection();
		$arrayCollection->models = $resultArray;
		$arrayCollection->count = $result_count["count"];;
		
		return $arrayCollection;

	}
	
	/**
	*Delte model by id
	*/
	public function deleteModel($modelID) 
	{
		$query_data = "UPDATE vehicle_model SET deleted=1 WHERE model_id='".$modelID."'";
		$result = mysql_query($query_data) or die($query_data);
		
		return $result;
	
	}
	
	/**
	*Add a new model
	*
	*/
	public function addModel($modelVO)
	{
	
		$queryString = "INSERT INTO vehicle_model(`model_id`,`make_id`,`model_name`, `model_description`,`created_by`, `create_date`) VALUES(NULL,'".$modelVO->makeID."','".$modelVO->model."','".$modelVO->description."','".$modelVO->createdBy."',now() )";
	
		$result = mysql_query($queryString) or die($queryString);
		return $result;
	}
	
	/**
	*Edit Model
	*/
	public function editModel($modelVO)
	{
	
		$queryString = "UPDATE vehicle_model SET make_id='".$modelVO->makeID."', model_name='" .$modelVO->model. "', model_description='" .$modelVO->description. "', updated_by='" .$modelVO->editedBy. "', update_date=now() WHERE model_id=".$modelVO->modelID;
		$result = mysql_query($queryString) or die($queryString);          
		return $result;
		
	}
} 
?>